{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3e3b7c14-b46b-443d-ac9a-6171787bb012",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fab2197-eedf-488f-84a8-dc443fb32e8c",
   "metadata": {},
   "source": [
    "# 生成索引数组\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "c_|将切片对象平移到第二个轴上并置。\n",
    "r_|将切片对象平移到沿第一个轴的连接。\n",
    "s_|为数组建立索引元组的更好方法。\n",
    "nonzero(a)|返回非零元素的索引。\n",
    "where(condition, [x, y])|根据条件返回从x或y中选择的元素。\n",
    "indices(dimensions[, dtype, sparse])|返回表示网格索引的数组。\n",
    "ix_(*args)|从多个序列构造一个开放的网格。\n",
    "ogrid|nd_grid实例，它返回一个开放的多维“ meshgrid”。\n",
    "ravel_multi_index(multi_index, dims[, mode, …])|将边界模式应用于多索引，将索引数组的元组转换为平面索引的数组。\n",
    "unravel_index(indices, shape[, order])|将平面索引或平面索引数组转换为坐标数组的元组。\n",
    "diag_indices(n[, ndim])|返回索引以访问数组的主对角线。\n",
    "diag_indices_from(arr)|返回索引以访问n维数组的主对角线。\n",
    "mask_indices(n, mask_func[, k])|给定掩码函数，将索引返回到访问（n，n）数组。\n",
    "tril_indices(n[, k, m])|返回 (n, m) 数组下三角的索引。\n",
    "tril_indices_from(arr[, k])|返回arr下三角的索引。\n",
    "triu_indices(n[, k, m])|返回 (n, m) 数组上三角的索引。\n",
    "triu_indices_from(arr[, k])|返回arr的上三角的索引。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b31b4200-253f-4653-bdf0-844a40657aba",
   "metadata": {},
   "source": [
    "## numpy.c_ = <numpy.lib._index_tricks_impl.CClass object>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ca3f39d0-173e-4a6e-bc3d-104854a99492",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.c_[np.array([1,2,3]), np.array([4,5,6])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "922d7d40-8992-40d3-800f-00ca13446b82",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 0, 0, 4, 5, 6]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08ee9adb-aa8f-4b93-bf47-c70a5de28a5f",
   "metadata": {},
   "source": [
    "## numpy.r_ = <numpy.lib._index_tricks_impl.RClass object>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "36201c4c-7e80-40f7-a471-bce5c9c59990",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 0, 0, 4, 5, 6])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.r_[np.array([1,2,3]), 0, 0, np.array([4,5,6])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6f9ef400-05f2-41de-9790-b4022bdc8e69",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1. , -0.6, -0.2,  0.2,  0.6,  1. ,  0. ,  0. ,  0. ,  5. ,  6. ])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.r_[-1:1:6j, [0]*3, 5, 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1432d82e-6508-4ccd-9d16-2a3632a78943",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 0, 1, 2],\n",
       "       [3, 4, 5, 3, 4, 5]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[0, 1, 2], [3, 4, 5]])\n",
    "np.r_['-1', a, a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0eecb249-6970-490e-89a8-01de3b36c6ed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.r_['0,2', [1,2,3], [4,5,6]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b10a033d-36ea-402f-a2b4-14d68dcec231",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4],\n",
       "       [5],\n",
       "       [6]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.r_['0,2,0', [1,2,3], [4,5,6]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "38a3a2b2-4e6e-4b6c-b8bc-a45d2eb258f8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.r_['1,2,0', [1,2,3], [4,5,6]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "16c12e2d-56d3-48d7-b211-4557aed68208",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2, 3, 4, 5, 6]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.r_['r',[1,2,3], [4,5,6]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e2db0a0-dccc-4d87-b7c4-75fb9b5c4797",
   "metadata": {},
   "source": [
    "## numpy.s_ = <numpy.lib._index_tricks_impl.IndexExpression object>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1515a72a-6f6b-4cb2-ba40-e739659e410f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "slice(2, None, 2)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.s_[2::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d129c334-3fc4-49ed-a042-cc3a7e62a1a3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(slice(2, None, 2),)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.index_exp[2::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a49a2f32-1e21-4e23-b480-d74d6764bd5c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([0, 1, 2, 3, 4])[np.s_[2::2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0f0d893-7e45-4507-bfaf-bb1b497cb716",
   "metadata": {},
   "source": [
    "## numpy.nonzero(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2160e1a1-8ece-444a-ba95-39fee79bcb3e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 0, 0],\n",
       "       [0, 4, 0],\n",
       "       [5, 6, 0]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d5f2792a-c6d3-400c-b27f-55728a17e747",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1, 2, 2]), array([0, 1, 0, 1]))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.nonzero(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8368f97e-27a4-4f5f-b72f-f4fadb4a20db",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5, 6])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[np.nonzero(x)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9de550a0-5fee-42ca-b4a2-2cc667d56c5f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [1, 1],\n",
       "       [2, 0],\n",
       "       [2, 1]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(np.nonzero(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "87ea6597-5028-427c-9114-8bcfa6576104",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False],\n",
       "       [ True,  True,  True],\n",
       "       [ True,  True,  True]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "a > 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fce779d7-bb44-40cd-a4ee-0222fee1c2eb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.nonzero(a > 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b12fdbf6-45b3-4b47-ae24-d9d31183d292",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[np.nonzero(a > 3)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "fa6dc097-f092-4940-a8aa-5500baf85f33",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[a > 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6cd3d32a-a8f7-4dfa-90c0-7c2709881a00",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(a > 3).nonzero()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b9f5c14-ddd6-46f8-b90b-5f918c425f29",
   "metadata": {},
   "source": [
    "## numpy.where(condition, [x, y, ]/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1c298097-2b88-4a77-bdf6-502daacad0b2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b63a2327-377f-46f4-a2bf-24c4f755448f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(a < 5, a, 10*a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "333789f6-7f46-47f5-92d1-d974367c0a2d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 8],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where([[True, False], [True, True]],\n",
    "         [[1, 2], [3, 4]],\n",
    "         [[9, 8], [7, 6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8142bf94-64c9-4f28-8a70-2a6fce2a7716",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10,  0,  0,  0],\n",
       "       [10, 11,  1,  1],\n",
       "       [10, 11, 12,  2]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, y = np.ogrid[:3, :4]\n",
    "np.where(x < y, x, 10 + y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6b0afc72-e3ef-48db-a8dd-52af798aeefd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [ 0,  2, -1],\n",
       "       [ 0,  3, -1]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[0, 1, 2],\n",
    "              [0, 2, 4],\n",
    "              [0, 3, 6]])\n",
    "np.where(a < 4, a, -1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbbd335c-926f-41e1-8cae-002d0244c2dc",
   "metadata": {},
   "source": [
    "## numpy.ix_(*args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "be262600-27b1-49f3-95aa-3f933f0c7a75",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10).reshape(2, 5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a3e8300c-e7e5-480c-aac5-50c097577352",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[0],\n",
       "        [1]]),\n",
       " array([[2, 4]]))"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ixgrid = np.ix_([0, 1], [2, 4])\n",
    "ixgrid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "42be372c-4042-4911-9fdb-15834c0ec0fa",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((2, 1), (1, 2))"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ixgrid[0].shape, ixgrid[1].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "84f7529b-afde-4c8d-afbf-53fc07f8794a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4],\n",
       "       [7, 9]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[ixgrid]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "11ab14b9-0fa0-495a-83a9-06d2257cffeb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4],\n",
       "       [7, 9]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ixgrid = np.ix_([True, True], [2, 4])\n",
    "a[ixgrid]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7c70f4e8-1482-4b67-b070-8b0e8a1f83ab",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4],\n",
       "       [7, 9]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ixgrid = np.ix_([True, True], [False, False, True, False, True])\n",
    "a[ixgrid]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0c79af4-e0f1-4e83-a11a-a6ad235564c7",
   "metadata": {},
   "source": [
    "## numpy.ogrid = <numpy.lib._index_tricks_impl.OGridClass object>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5b7e0558-c84c-4638-945d-a3e6c95d64c3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1. , -0.5,  0. ,  0.5,  1. ])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ogrid[-1:1:5j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8efb9d25-ec1f-4dc8-9fe1-ba7d5757e62c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0],\n",
       "        [1],\n",
       "        [2],\n",
       "        [3],\n",
       "        [4]]),\n",
       " array([[0, 1, 2, 3, 4]])]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ogrid[0:5,0:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e8f8067-8eb7-4d41-a9f3-854ce0059d1b",
   "metadata": {},
   "source": [
    "## numpy.ravel_multi_index(multi_index, dims, mode='raise', order='C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0c99c56a-93c2-4fd7-81db-600c077dc74f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([22, 41, 37])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([[3,6,6],[4,5,1]])\n",
    "np.ravel_multi_index(arr, (7,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d79c28c8-bef8-485b-92ac-86f3990744c4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([31, 41, 13])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ravel_multi_index(arr, (7,6), order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "cb750327-0e19-4305-a461-4d95abb6a939",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([22, 23, 19])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ravel_multi_index(arr, (4,6), mode='clip')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c0eeebae-167f-43df-be06-ae8b8cbd5d9a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12, 13, 13])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ravel_multi_index(arr, (4,4), mode=('clip','wrap'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "e90b1b4c-aac2-45d0-9c33-12eb2cd8fdd1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1621"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ravel_multi_index((3,1,4,1), (6,7,8,9))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e673b778-7c0f-438f-b318-e5e257ec24bb",
   "metadata": {},
   "source": [
    "## numpy.unravel_index(indices, shape, order='C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "27b9ec96-f50a-4494-9db0-973ada2eaa28",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([3, 6, 6]), array([4, 5, 1]))"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unravel_index([22, 41, 37], (7,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e990f2a9-71ef-4b00-86c3-e7c323b32211",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([3, 6, 6]), array([4, 5, 1]))"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unravel_index([31, 41, 13], (7,6), order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "87550073-5a4a-4355-a60e-befee2bbb68b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 1, 4, 1)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unravel_index(1621, (6,7,8,9))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e85ce01-ff8e-4e71-8e68-4a3cf95aa593",
   "metadata": {},
   "source": [
    "## numpy.diag_indices(n, ndim=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "7c81d26d-1bf0-4ddc-931c-00ec95482eb1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1, 2, 3]), array([0, 1, 2, 3]))"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "di = np.diag_indices(4)\n",
    "di"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "ade7eb42-851a-4843-ad10-b9d387d3a4a2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(16).reshape(4, 4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "c766dece-d3db-48d6-8b3e-df0f3c6e495c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[100,   1,   2,   3],\n",
       "       [  4, 100,   6,   7],\n",
       "       [  8,   9, 100,  11],\n",
       "       [ 12,  13,  14, 100]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[di] = 100\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "4d5788e0-c8d3-44dc-aa12-42590e911eef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1]), array([0, 1]), array([0, 1]))"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3 = np.diag_indices(2, 3)\n",
    "d3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "7ac22331-96f2-4bf2-a798-e7c02cc0199d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 0],\n",
       "        [0, 0]],\n",
       "\n",
       "       [[0, 0],\n",
       "        [0, 1]]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.zeros((2, 2, 2), dtype=int)\n",
    "a[d3] = 1\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20a95996-a587-4481-b38c-885a6fbdb981",
   "metadata": {},
   "source": [
    "## numpy.diag_indices_from(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "3c88cbfe-32ec-47f1-bec4-4a1e7ec8ab37",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(16).reshape(4, 4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "6def778e-4121-46f2-86bc-18b37f4549ed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1, 2, 3]), array([0, 1, 2, 3]))"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "di = np.diag_indices_from(a)\n",
    "di"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "73c9f938-a1a5-4750-9c20-bf7c75afa9a2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  5, 10, 15])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[di]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "f6ecad32-fa98-4b06-85f9-28f8451969c5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1, 2, 3]), array([0, 1, 2, 3]))"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diag_indices(a.shape[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a465113-4da4-4c6b-8f28-47d6faadeda9",
   "metadata": {},
   "source": [
    "## numpy.mask_indices(n, mask_func, k=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "467a3a43-d051-47ce-aaa0-5c43e2bcc774",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "iu = np.mask_indices(3, np.triu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "54ca4ab0-d883-4a56-81a2-718ac01abfed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(9).reshape(3, 3)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "3cbbfd41-aa58-44a0-8363-b366cd331f13",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 4, 5, 8])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[iu]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "bbb76d1a-066a-40f6-9b65-97d1f1c4b9d4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "iu1 = np.mask_indices(3, np.triu, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "91edd7ef-386b-41e0-94d9-aaa566963f69",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 5])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[iu1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7a4748d-6b4a-4d30-bb71-fbfec6d26a0e",
   "metadata": {},
   "source": [
    "## numpy.tril_indices(n, k=0, m=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "b6309a4b-0e34-4b01-aaec-2596720ed34f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "il1 = np.tril_indices(4)\n",
    "il2 = np.tril_indices(4, 2)\n",
    "a = np.arange(16).reshape(4, 4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3006b6b3-382f-46c1-ab57-6719db74534b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  4,  5,  8,  9, 10, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[il1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "1f229a06-b755-4288-84c8-2fa018699fd8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1,  1,  2,  3],\n",
       "       [-1, -1,  6,  7],\n",
       "       [-1, -1, -1, 11],\n",
       "       [-1, -1, -1, -1]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[il1] = -1\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "d6e5a937-b6c9-499d-81a6-b23f7d227d34",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-10, -10, -10,   3],\n",
       "       [-10, -10, -10, -10],\n",
       "       [-10, -10, -10, -10],\n",
       "       [-10, -10, -10, -10]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[il2] = -10\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4acba86-efce-42b3-b601-aee78bb9b9f3",
   "metadata": {},
   "source": [
    "## numpy.tril_indices_from(arr, k=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "ba707dc9-336a-4a85-92b6-5602630bbf52",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(16).reshape(4, 4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "98732400-012a-4569-8912-653f99e9a532",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1, 1, 2, 2, 2, 3, 3, 3, 3]), array([0, 0, 1, 0, 1, 2, 0, 1, 2, 3]))"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trili = np.tril_indices_from(a)\n",
    "trili"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "c23965df-e62d-4b29-9c53-e05e368b5e29",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  4,  5,  8,  9, 10, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[trili]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "afc8be23-dc6c-4537-88ff-f0ef43b220dd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1, 1, 2, 2, 2, 3, 3, 3, 3]), array([0, 0, 1, 0, 1, 2, 0, 1, 2, 3]))"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tril_indices(a.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "f40719cd-7ccb-42af-b19c-fd6d86bee789",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  4,  5,  6,  8,  9, 10, 11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trili1 = np.tril_indices_from(a, k=1)\n",
    "a[trili1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c4d5995-2161-4a4c-9b20-220b01278465",
   "metadata": {},
   "source": [
    "## numpy.triu_indices(n, k=0, m=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "e6f1ac50-1c95-430a-ab12-a9089fff1d77",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iu1 = np.triu_indices(4)\n",
    "iu2 = np.triu_indices(4, 2)\n",
    "a = np.arange(16).reshape(4, 4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "c834418e-fea7-428c-9b05-1681a04f3140",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  5,  6,  7, 10, 11, 15])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[iu1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "929fb11e-3660-44e5-a6b8-b6c6bb23a25f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1, -1, -1, -1],\n",
       "       [ 4, -1, -1, -1],\n",
       "       [ 8,  9, -1, -1],\n",
       "       [12, 13, 14, -1]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[iu1] = -1\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "200c24f1-6df1-4a76-af96-1f6a388adec5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ -1,  -1, -10, -10],\n",
       "       [  4,  -1,  -1, -10],\n",
       "       [  8,   9,  -1,  -1],\n",
       "       [ 12,  13,  14,  -1]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[iu2] = -10\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c80d0dcb-d21e-4484-a249-eb89d4a5498f",
   "metadata": {},
   "source": [
    "## numpy.triu_indices_from(arr, k=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "70b4bbc0-91f2-436b-aa37-21b34da4e66d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(16).reshape(4, 4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "cf357e56-f4f8-4a11-b4ce-1eede90104f3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]), array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "triui = np.triu_indices_from(a)\n",
    "triui"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "88260b92-c7fa-4ad1-a5a5-b719146b69f2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  5,  6,  7, 10, 11, 15])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[triui]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "e25a3315-754a-4dab-a569-9e4bbb9dc528",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]), array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.triu_indices(a.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "0dc2c3a6-2047-42d5-b057-dba1c3178f9e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  6,  7, 11])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "triuim1 = np.triu_indices_from(a, k=1)\n",
    "a[triuim1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33860f34-7f66-4137-aec3-97a2aec3496c",
   "metadata": {},
   "source": [
    "# 类似于索引的操作\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "take(a, indices[, axis, out, mode])|沿轴从数组中获取元素。\n",
    "take_along_axis(arr, indices, axis)|通过匹配1d索引和数据切片从输入数组中获取值。\n",
    "choose(a, choices[, out, mode])|从索引数组和一组数组中构造一个数组以供选择。\n",
    "compress(condition, a[, axis, out])|沿给定轴返回数组的选定切片。\n",
    "diag(v[, k])|提取对角线或构造对角线阵列。\n",
    "diagonal(a[, offset, axis1, axis2])|返回指定的对角线。\n",
    "select(condlist, choicelist[, default])|根据条件返回从Choicelist中的元素中提取的数组。\n",
    "lib.stride_tricks.as_strided(x[, shape, …])|使用给定的形状和步幅在阵列中创建视图。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02162b15-09b6-4476-a07f-edb726b8f4fd",
   "metadata": {},
   "source": [
    "## numpy.take(a, indices, axis=None, out=None, mode='raise')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "b6e7725d-50cb-4267-9bd7-ef6ffccfa582",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 3, 6])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [4, 3, 5, 7, 6, 8]\n",
    "indices = [0, 1, 4]\n",
    "np.take(a, indices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "670e5126-39da-43d3-bccf-6847585a0972",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 3, 6])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array(a)\n",
    "a[indices]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "72c4d22d-d503-49a0-b9b4-e025ed3e56a7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 3],\n",
       "       [5, 7]])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.take(a, [[0, 1], [2, 3]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "394decd9-d386-467c-b3db-8b2bca85db6d",
   "metadata": {},
   "source": [
    "## numpy.take_along_axis(arr, indices, axis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "86438cbc-5fdb-468f-8674-96cdd8c8b5c2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "a = np.array([[10, 30, 20], [60, 40, 50]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "59f979aa-8627-4fff-8658-fade627ab9cc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 20, 30],\n",
       "       [40, 50, 60]])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(a, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "2ee28587-ab54-43d1-a851-64c81b827128",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 1],\n",
       "       [1, 2, 0]])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ai = np.argsort(a, axis=1)\n",
    "ai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "13d21442-eb10-4291-a98c-88521166b6a9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 20, 30],\n",
       "       [40, 50, 60]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.take_along_axis(a, ai, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "23b2f616-3099-4fa7-b222-e46740c9c2fe",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[30],\n",
       "       [60]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(a, axis=1, keepdims=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "b96beaca-679b-49c6-9727-3a2dabc91a1d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 20, 30],\n",
       "       [40, 50, 60]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.take_along_axis(a, ai, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "357f08f0-ef79-427f-9556-a92d6fc4359c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 20, 30],\n",
       "       [40, 50, 60]])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.take_along_axis(a, ai, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d047a8af-22d1-49e7-8ca6-5a384233ac54",
   "metadata": {},
   "source": [
    "## numpy.choose(a, choices, out=None, mode='raise')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "9ef5dda3-1c24-4efa-b320-8f854cdd4183",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20, 31, 12,  3])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choices = [[0, 1, 2, 3], [10, 11, 12, 13],\n",
    "  [20, 21, 22, 23], [30, 31, 32, 33]]\n",
    "np.choose([2, 3, 1, 0], choices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "5ce751ee-36c5-4690-b9c6-dd86b3343d79",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20, 31, 12,  3])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.choose([2, 4, 1, 0], choices, mode='clip')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "522a2a61-f0a9-4562-959d-e5064ca127b2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20,  1, 12,  3])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.choose([2, 4, 1, 0], choices, mode='wrap')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "a6afa37f-63c7-4a4c-bfaa-6b0c1035d595",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 10, -10,  10],\n",
       "       [-10,  10, -10],\n",
       "       [ 10, -10,  10]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]\n",
    "choices = [-10, 10]\n",
    "np.choose(a, choices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "7028d207-e6e7-4669-ba7a-e1f466742e80",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  1,  1,  1,  1],\n",
       "        [ 2,  2,  2,  2,  2],\n",
       "        [ 3,  3,  3,  3,  3]],\n",
       "\n",
       "       [[-1, -2, -3, -4, -5],\n",
       "        [-1, -2, -3, -4, -5],\n",
       "        [-1, -2, -3, -4, -5]]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([0, 1]).reshape((2,1,1))\n",
    "c1 = np.array([1, 2, 3]).reshape((1,3,1))\n",
    "c2 = np.array([-1, -2, -3, -4, -5]).reshape((1,1,5))\n",
    "np.choose(a, (c1, c2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8cc9610-dfc1-49d6-9f4a-5eb397973f81",
   "metadata": {},
   "source": [
    "## numpy.compress(condition, a, axis=None, out=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "423f1486-efba-4e15-915f-2c938ddde748",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "9851995e-ff51-4d0b-8d66-a531a39922fe",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.compress([0, 1], a, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "50a931bd-0dca-4d88-96ae-8fb8d4982d02",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.compress([False, True, True], a, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "192f7337-544e-46a9-bdac-9d2df65d75d3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2],\n",
       "       [4],\n",
       "       [6]])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.compress([False, True], a, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "aa560a58-f099-4348-ab45-b3f9b78b26ef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.compress([False, True], a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27465752-cad6-4743-8185-2ca03d2811d1",
   "metadata": {},
   "source": [
    "## numpy.diag(v, k=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "21f08334-a27f-4a16-9e4f-ec8b5f9153f6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(9).reshape((3,3))\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "7ed54b38-6c74-4027-aceb-b812e2ecaffc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 4, 8])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diag(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "96ab4e9a-4e2f-4c11-b059-30405a817bfb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 5])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diag(x, k=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "73f2eb5a-25ab-4253-9042-1da6e6cac2df",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 7])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diag(x, k=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "ec3a5b4a-3384-4c2b-8aa0-4bcda412e60d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [0, 4, 0],\n",
       "       [0, 0, 8]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diag(np.diag(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17e3a63c-da1c-430d-a2c9-596693192dcd",
   "metadata": {},
   "source": [
    "## numpy.diagonal(a, offset=0, axis1=0, axis2=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "5a12f1f8-2c4b-4ffd-a2a1-99e2f34dd582",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(4).reshape(2,2)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "6c642bc7-9227-4978-b723-bdc5eea830ba",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 3])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.diagonal()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "2d9e9e82-1650-47ec-8c76-7de06192315c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.diagonal(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "8e6e4c44-1259-42be-a891-b5bc8e20f600",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1],\n",
       "        [2, 3]],\n",
       "\n",
       "       [[4, 5],\n",
       "        [6, 7]]])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(8).reshape(2,2,2); a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "88091477-41e7-47dc-85a2-27f7b973c9ae",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 6],\n",
       "       [1, 7]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.diagonal(0,0,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "81339016-4469-4ff9-8709-0dd871c4d717",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2],\n",
       "       [4, 6]])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:,:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "90f84608-9bcf-4e91-aac0-7346742aafe6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3],\n",
       "       [5, 7]])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:,:,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "21f66250-396b-4ded-bc8e-fbe1449a391b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(9).reshape(3, 3)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "ac0659b1-c6a3-402d-8fd5-3d4a5f08cb3e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 6])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.fliplr(a).diagonal()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "8905fc97-fb26-4800-888c-14aa3afde39c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 4, 2])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.flipud(a).diagonal()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d98dacf8-0d68-4b3e-b8bd-1184dc2469c0",
   "metadata": {},
   "source": [
    "## numpy.select(condlist, choicelist, default=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "7330f6e5-d3e3-464f-9951-f0b831ae8a70",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2, 42, 16, 25])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(6)\n",
    "condlist = [x<3, x>3]\n",
    "choicelist = [x, x**2]\n",
    "np.select(condlist, choicelist, 42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "59ef8e33-812b-4faa-8490-247f305e2e41",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4, 25])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "condlist = [x<=4, x>3]\n",
    "choicelist = [x, x**2]\n",
    "np.select(condlist, choicelist, 55)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0704531d-beff-4666-8f9d-7221d0e5b353",
   "metadata": {},
   "source": [
    "## lib.stride_tricks.as_strided(x, shape=None, strides=None, subok=False, writeable=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69cbcc05-330b-421c-8638-3c213050bc79",
   "metadata": {},
   "source": [
    "# 将数据插入数组\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "place(arr, mask, vals)|基于条件值和输入值更改数组的元素。\n",
    "put(a, ind, v[, mode])|用给定值替换数组的指定元素。\n",
    "put_along_axis(arr, indices, values, axis)|通过匹配1D索引和数据切片将值放入目标数组中。\n",
    "putmask(a, mask, values)|基于条件值和输入值更改数组的元素。\n",
    "fill_diagonal(a, val[, wrap])|填充任意维数的给定数组的主对角线。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23f1c0c9-e395-4006-ac18-18681cc1f9f3",
   "metadata": {},
   "source": [
    "## numpy.place(arr, mask, vals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "600d0f5c-5474-42b7-b6c0-664ff03bc39c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [44, 55, 44]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(6).reshape(2, 3)\n",
    "np.place(arr, arr>2, [44, 55])\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09943916-e054-43ec-b91d-4c67ca14dab1",
   "metadata": {},
   "source": [
    "## numpy.put(a, ind, v, mode='raise')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "06047202-c7fe-4e4c-bbd4-df3a4a9f16fb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-44,   1, -55,   3,   4])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(5)\n",
    "np.put(a, [0, 2], [-44, -55])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "ca7feb93-5239-4b39-834e-8c0bcf861c16",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3, -5])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(5)\n",
    "np.put(a, 22, -5, mode='clip')\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67868883-2b0e-46f2-a49f-6a9c3f939746",
   "metadata": {},
   "source": [
    "## numpy.put_along_axis(arr, indices, values, axis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "6985f3ed-0f76-4c39-85b3-0e3e74c9e2c1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[99, 99, 99],\n",
       "       [99, 99, 99]])"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[10, 30, 20], [60, 40, 50]])\n",
    "np.put_along_axis(a, ai, 99, axis=1)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "846c225d-85aa-47f6-982e-11466ad7e355",
   "metadata": {},
   "source": [
    "## numpy.putmask(a, mask, values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "e53de088-8212-4700-a805-5db35b52593d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [ 9, 16, 25]])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(6).reshape(2, 3)\n",
    "np.putmask(x, x>2, x**2)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "6751f794-efd2-42bf-91d9-389bb46afec2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0,   1, -33, -44, -33])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(5)\n",
    "np.putmask(x, x>1, [-33, -44])\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03c272d8-31d5-4154-a1d3-7c75dbc05f24",
   "metadata": {},
   "source": [
    "## numpy.fill_diagonal(a, val, wrap=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "2f26370a-9f6e-41fa-9eea-a61e0cb9abc2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 0, 0],\n",
       "       [0, 5, 0],\n",
       "       [0, 0, 5]])"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.zeros((3, 3), int)\n",
    "np.fill_diagonal(a, 5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "92aa689e-50df-4f0c-bea4-8e7c76ff94bb",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "a = np.zeros((3, 3, 3, 3), int)\n",
    "np.fill_diagonal(a, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "d4ab0974-4ebd-4891-9a8d-6cf5e6bfbeae",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 0, 0],\n",
       "       [0, 0, 0],\n",
       "       [0, 0, 0]])"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0, 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "54737437-c35f-478c-9437-96934bb5388c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [0, 4, 0],\n",
       "       [0, 0, 0]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "9b59c0eb-3c5a-4cfe-b803-a2aee560ecf3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [0, 0, 0],\n",
       "       [0, 0, 4]])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "8dfc91ec-b8e1-458c-bea9-479dd67e9f91",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 0, 0],\n",
       "       [0, 4, 0],\n",
       "       [0, 0, 4],\n",
       "       [0, 0, 0],\n",
       "       [0, 0, 0]])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.zeros((5, 3), int)\n",
    "np.fill_diagonal(a, 4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "f4449583-2b31-4904-826e-4ca0b67f918a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 0, 0],\n",
       "       [0, 4, 0],\n",
       "       [0, 0, 4],\n",
       "       [0, 0, 0],\n",
       "       [4, 0, 0]])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.zeros((5, 3), int)\n",
    "np.fill_diagonal(a, 4, wrap=True)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "542f3b03-2a09-44bb-8153-73dc756014db",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 0, 0, 0, 0],\n",
       "       [0, 4, 0, 0, 0],\n",
       "       [0, 0, 4, 0, 0]])"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.zeros((3, 5), int)\n",
    "np.fill_diagonal(a, 4, wrap=True)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "bf0fee68-ca4b-46e4-bc52-c65e2a09c5f0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 1],\n",
       "       [0, 2, 0],\n",
       "       [3, 0, 0]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.zeros((3, 3), int);\n",
    "np.fill_diagonal(np.fliplr(a), [1,2,3])  # Horizontal flip\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "f70c36e4-8b60-41f3-8850-507bf8581a33",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 3],\n",
       "       [0, 2, 0],\n",
       "       [1, 0, 0]])"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.fill_diagonal(np.flipud(a), [1,2,3])  # Vertical flip\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c28b55d7-003e-472e-a1cd-149e7543f3fe",
   "metadata": {},
   "source": [
    "# 迭代数组\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "nditer|高效的多维迭代器对象来迭代数组。\n",
    "ndenumerate(arr)|多维索引迭代器。\n",
    "ndindex(*shape)|用于索引数组的N维迭代器对象。\n",
    "nested_iters()|创建用于嵌套循环的nditer\n",
    "flatiter|要迭代数组的平面迭代器对象。\n",
    "lib.Arrayterator(var[, buf_size])|大数组的缓冲迭代器。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bbed26a-fd9f-4eb2-9509-4947a2213418",
   "metadata": {},
   "source": [
    "## class numpy.nditer(op, flags=None, op_flags=None, op_dtypes=None, order='K', casting='safe', op_axes=None, itershape=None, buffersize=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "b6680160-aca3-445e-95c3-605fed80960d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def iter_add_py(x, y, out=None):\n",
    "    addop = np.add\n",
    "    it = np.nditer([x, y, out], [],\n",
    "                [['readonly'], ['readonly'], ['writeonly','allocate']])\n",
    "    with it:\n",
    "        for (a, b, c) in it:\n",
    "            addop(a, b, out=c)\n",
    "        return it.operands[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "b9f3b153-3b1f-4452-9811-5ac8bc3affb0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def iter_add(x, y, out=None):\n",
    "   addop = np.add\n",
    "   it = np.nditer([x, y, out], [],\n",
    "               [['readonly'], ['readonly'], ['writeonly','allocate']])\n",
    "   with it:\n",
    "       while not it.finished:\n",
    "           addop(it[0], it[1], out=it[2])\n",
    "           it.iternext()\n",
    "       return it.operands[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "6677dccc-f77e-44a5-8931-4dc67d46e7d8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def outer_it(x, y, out=None):\n",
    "    mulop = np.multiply\n",
    "    it = np.nditer([x, y, out], ['external_loop'],\n",
    "            [['readonly'], ['readonly'], ['writeonly', 'allocate']],\n",
    "            op_axes=[list(range(x.ndim)) + [-1] * y.ndim,\n",
    "                     [-1] * x.ndim + list(range(y.ndim)),\n",
    "                     None])\n",
    "    with it:\n",
    "        for (a, b, c) in it:\n",
    "            mulop(a, b, out=c)\n",
    "        return it.operands[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "ecc1d603-2070-49c5-b29b-fa3554b23233",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 4, 6]])"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(2)+1\n",
    "b = np.arange(3)+1\n",
    "outer_it(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "8707a438-7e6a-439a-8372-526f88f00ed3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def luf(lamdaexpr, *args, **kwargs):\n",
    "   '''luf(lambdaexpr, op1, ..., opn, out=None, order='K', casting='safe', buffersize=0)'''\n",
    "   nargs = len(args)\n",
    "   op = (kwargs.get('out',None),) + args\n",
    "   it = np.nditer(op, ['buffered','external_loop'],\n",
    "           [['writeonly','allocate','no_broadcast']] +\n",
    "                           [['readonly','nbo','aligned']]*nargs,\n",
    "           order=kwargs.get('order','K'),\n",
    "           casting=kwargs.get('casting','safe'),\n",
    "           buffersize=kwargs.get('buffersize',0))\n",
    "   while not it.finished:\n",
    "       it[0] = lamdaexpr(*it[1:])\n",
    "       it.iternext()\n",
    "   return it.operands[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "3ede2230-8885-4ae6-9cd1-429b3957e778",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.5,  1.5,  4.5,  9.5, 16.5])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(5)\n",
    "b = np.ones(5)\n",
    "luf(lambda i,j:i*i + j/2, a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "1e463eb9-c28e-48d3-8807-aa2b0fdb19fb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([-1, -2, -3], dtype=int32), array([-1., -2., -3.], dtype=float32))"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(6, dtype='i4')[::-2]\n",
    "with np.nditer(a, [],\n",
    "       [['writeonly', 'updateifcopy']],\n",
    "       casting='unsafe',\n",
    "       op_dtypes=[np.dtype('f4')]) as i:\n",
    "   x = i.operands[0]\n",
    "   x[:] = [-1, -2, -3]\n",
    "\n",
    "a, x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a56bdd9-be16-4750-9905-f6cc39650dce",
   "metadata": {},
   "source": [
    "## class numpy.ndenumerate(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "3738c0eb-3ff1-4bba-b5d0-393280802a0a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0) 1\n",
      "(0, 1) 2\n",
      "(1, 0) 3\n",
      "(1, 1) 4\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1, 2], [3, 4]])\n",
    "for index, x in np.ndenumerate(a):\n",
    "    print(index, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bebad1da-4696-4717-85ec-ab2e9b720e1e",
   "metadata": {},
   "source": [
    "## class numpy.ndindex(*shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "acf6fd9d-410c-439b-9c22-a74285dec3ed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0, 0)\n",
      "(0, 1, 0)\n",
      "(1, 0, 0)\n",
      "(1, 1, 0)\n",
      "(2, 0, 0)\n",
      "(2, 1, 0)\n"
     ]
    }
   ],
   "source": [
    "for index in np.ndindex(3, 2, 1):\n",
    "    print(index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "9a47aea3-f131-4272-8401-88ed90a78784",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0, 0)\n",
      "(0, 1, 0)\n",
      "(1, 0, 0)\n",
      "(1, 1, 0)\n",
      "(2, 0, 0)\n",
      "(2, 1, 0)\n"
     ]
    }
   ],
   "source": [
    "for index in np.ndindex((3, 2, 1)):\n",
    "    print(index)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bcd634c-783f-4985-bb55-fe42bebf894a",
   "metadata": {},
   "source": [
    "## numpy.nested_iters(op, axes, flags=None, op_flags=None, op_dtypes=None, order='K', casting='safe', buffersize=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "b5e432c8-17db-440c-a945-c394baea620f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0,)\n",
      " (0, 0) 0\n",
      " (0, 1) 1\n",
      " (1, 0) 6\n",
      " (1, 1) 7\n",
      "(1,)\n",
      " (0, 0) 2\n",
      " (0, 1) 3\n",
      " (1, 0) 8\n",
      " (1, 1) 9\n",
      "(2,)\n",
      " (0, 0) 4\n",
      " (0, 1) 5\n",
      " (1, 0) 10\n",
      " (1, 1) 11\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(12).reshape(2, 3, 2)\n",
    "i, j = np.nested_iters(a, [[1], [0, 2]], flags=[\"multi_index\"])\n",
    "for x in i:\n",
    "     print(i.multi_index)\n",
    "     for y in j:\n",
    "         print('', j.multi_index, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6f6e9a0-2790-4c48-898b-c782787bcbad",
   "metadata": {},
   "source": [
    "## class numpy.flatiter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "4b70788a-96d5-4ee5-b13e-3388b2605add",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.flatiter"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(6).reshape(2, 3)\n",
    "fl = x.flat\n",
    "type(fl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "993b3b04-910e-4e7d-96fb-d75c306e03e7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for item in fl:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "7d440372-4bd6-4f23-9a93-cc9cd7259af6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3])"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fl[2:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1935d216-db7e-4c4f-ac6b-50ffb60e6219",
   "metadata": {},
   "source": [
    "## class numpy.lib.Arrayterator(var, buf_size=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "39cacb50-b304-4d08-937c-b0a0aa385480",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4, 5, 6)"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(3 * 4 * 5 * 6).reshape(3, 4, 5, 6)\n",
    "a_itor = np.lib.Arrayterator(a, 2)\n",
    "a_itor.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "53007192-1561-47ec-aa59-db3efe2189cb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[[0 1]]]] (1, 1, 1, 2)\n"
     ]
    }
   ],
   "source": [
    "for subarr in a_itor:\n",
    "    if not subarr.all():\n",
    "        print(subarr, subarr.shape) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca6fc742-be2b-41a5-84f6-d6299c2eee7f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
